<script>
	export default {
		data(){
			return{
				socket:null,
				userinfo:[]
			}
		},
		onLaunch: function() {
			console.log('App Launch')
			// #ifdef APP-PLUS  
			plus.runtime.getProperty(plus.runtime.appid, function(widgetInfo) {  
				console.log(`当前版本：${widgetInfo.version}`)
				console.log(`name：${widgetInfo.name}`)
			    /* uni.request({  
			        url: 'http://www.example.com/update/',  
			        data: {  
			            version: widgetInfo.version,  
			            name: widgetInfo.name  
			        },  
			        success: (result) => {  
			            var data = result.data;  
			            if (data.update && data.wgtUrl) {  
			                uni.downloadFile({  
			                    url: data.wgtUrl,  
			                    success: (downloadResult) => {  
			                        if (downloadResult.statusCode === 200) {  
			                            plus.runtime.install(downloadResult.tempFilePath, {  
			                                force: true  
			                            }, function() {  
			                                console.log('install success...');  
			                                plus.runtime.restart();  
			                            }, function(e) {  
			                                console.error('install fail...');  
			                            });  
			                        }  
			                    }  
			                });  
			            }  
			        }  
			    });  */
			});  
			// #endif
			
		},
		onShow: function() {
			console.log('App Show')
			this.openWs()
			//可以每隔一定时间就监测一次
			setInterval(()=>{
				this.checkWs();
			},60000);
			// 保持屏幕常亮
			uni.setKeepScreenOn({
			    keepScreenOn: true
			});
		},
		onHide: function() {
			console.log('App Hide')
			
		},
		methods:{
			//socket创建和监听
			openWs(){
				try {
				    const value = uni.getStorageSync('userStatus');
				    if (value) {
						console.log(value)
				        this.userinfo = value
				    }
				} catch (e) {
				    // error
				}
				this.socket = uni.connectSocket({
			    	url: this.$api.socket+this.userinfo.username, //仅为示例，并非真实接口地址。
			    	complete: (res)=> {
						console.log('socket监听',res)
					}
				});
				//监听等事件...
			},
			//监测连接，如果断开就重连
			checkWs(){
				//心跳重连
				console.log('this.socket-----',this.socket.readyState)
				if([2,3].includes(this.socket.readyState)){
					//closing 或 closed
					console.log('socket重连----------------------')
					this.socket.close();
					this.socket = null;
					this.openWs();		
				}
			}
		}
	}
</script>

<style>
	/*每个页面公共css */
	/* 禁用iPhone中Safari的字号自动调整 */
	html {
	  -webkit-text-size-adjust: 100%;
	  -ms-text-size-adjust: 100%;
	  /* 解决IOS默认滑动很卡的情况 */
	  /* -webkit-overflow-scrolling: touch; */
	  height: 100%;
	  width: 100%;
	}
	
	/* 禁止缩放表单 */
	input[type="submit"],
	input[type="reset"],
	input[type="button"],
	input {
	  resize: none;
	  border: none;
	}
	
	/* 取消链接高亮  */
	* {
	  -webkit-tap-highlight-color: rgba(0, 0, 0, 0);
	  box-sizing: border-box;
	}
	
	/* 移动端点击a链接出现蓝色背景问题解决 */
	a:link,
	a:active,
	a:visited,
	a:hover {
	  background: none;
	  -webkit-tap-highlight-color: rgba(0, 0, 0, 0);
	  -webkit-tap-highlight-color: transparent;
	}
	
	
	html,
	body,
	p,
	ol,
	ul,
	li,
	dl,
	dt,
	dd,
	blockquote,
	figure,
	fieldset,
	legend,
	textarea,
	pre,
	iframe,
	hr,
	h1,
	h2,
	h3,
	h4,
	h5,
	h6 {
	  margin: 0;
	  padding: 0;
	}
	
	h1,
	h2,
	h3,
	h4,
	h5,
	h6 {
	  font-size: 100%;
	  font-weight: normal;
	}
	
	ul {
	  list-style: none;
	}
	
	button,
	input,
	select,
	textarea {
	  margin: 0;
	}
	
	html {
	  box-sizing: border-box;
	}
	
	/* *, *:before, *:after {
	  box-sizing: inherit;
	} */
	
	
</style>
